import { getLocal, setLocal } from "./local";
/**
 * @param {传入主题参数} value 
 * @returns 
 */
export const setTheme = (value) => {
  if (!value) return null;
  if (getLocal("windows-settings")) {
    const settings = getLocal("windows-settings")
    settings.theme = value;
    setLocal("windows-settings", settings)
  } else {
    setLocal("windows-settings", {
      theme: value,
    });
  }
}

/**
 * 通过 修改元素类名 实现主题修改
 * @param {传入主题参数 只有dark一种 } value 
 */
const setClass = (value) => {
  const element = document.querySelector("html");
  // classList 是一个获取元素类名的。contains是判断是否包含
  if (value == "dark") {
    if (element.classList.contains("dark")) {
      element.classList.remove("dark");
    } else {
      element.classList.add("dark");
    }
  } else {
    element.classList.remove("dark");
  }
}

/**
 *  通过浏览器存储获取主题颜色
 * @returns 返回值一定是light 
 */
export const getTheme = () => {
  if (getLocal("windows-settings")) {
    const settings = getLocal("windows-settings");
    if (settings.theme) {
      setClass(settings.theme);
      return settings.theme;
    } else {
      settings.theme = "light";
      setLocal("windows-settings", settings);
      setClass(settings.theme);
      return "light";
    }
  }
  setLocal("windows-settings", {
    theme: "light",
  })
  setClass("light");
  return "light";
}